home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / libelf-0.5 / libelf-0 / libelf-0.5.2 / end.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-19  |  2.5 KB  |  109 lines

  1. /*
  2. end.c - implementation of the elf_end(3) function.
  3. Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <private.h>
  21.  
  22. static void
  23. _elf_free(void *ptr) {
  24.     if (ptr) {
  25.     free(ptr);
  26.     }
  27. }
  28.  
  29. static void
  30. _elf_free_scns(Elf *elf, Elf_Scn *scn) {
  31.     Scn_Data *sd, *tmp;
  32.     Elf_Scn *freescn;
  33.  
  34.     for (freescn = NULL; scn; scn = scn->s_link) {
  35.     elf_assert(scn->s_magic == SCN_MAGIC);
  36.     elf_assert(scn->s_elf == elf);
  37.     for (sd = scn->s_data_1; sd; sd = tmp) {
  38.         tmp = sd->sd_link;
  39.         if (sd->sd_free_data) {
  40.         _elf_free(sd->sd_memdata);
  41.         }
  42.         if (sd->sd_freeme) {
  43.         free(sd);
  44.         }
  45.     }
  46.     if ((sd = scn->s_rawdata)) {
  47.         if (sd->sd_free_data) {
  48.         _elf_free(sd->sd_memdata);
  49.         }
  50.         if (sd->sd_freeme) {
  51.         free(sd);
  52.         }
  53.     }
  54.     if (scn->s_freeme) {
  55.         _elf_free(freescn);
  56.         freescn = scn;
  57.     }
  58.     }
  59.     _elf_free(freescn);
  60. }
  61.  
  62. int
  63. elf_end(Elf *elf) {
  64.     Elf **siblings;
  65.  
  66.     if (!elf) {
  67.     return 0;
  68.     }
  69.     elf_assert(elf->e_magic == ELF_MAGIC);
  70.     if (--elf->e_count) {
  71.     return elf->e_count;
  72.     }
  73.     if (elf->e_parent) {
  74.     elf_assert(elf->e_parent->e_magic == ELF_MAGIC);
  75.     elf_assert(elf->e_parent->e_kind == ELF_K_AR);
  76.     siblings = &elf->e_parent->e_members;
  77.     while (*siblings) {
  78.         if (*siblings == elf) {
  79.         *siblings = elf->e_link;
  80.         break;
  81.         }
  82.         siblings = &(*siblings)->e_link;
  83.     }
  84.     elf_end(elf->e_parent);
  85.     _elf_free(elf->e_arhdr);
  86.     }
  87.     else {
  88.     _elf_free(elf->e_data);
  89.     }
  90.     _elf_free_scns(elf, elf->e_scn_1);
  91.     if (elf->e_rawdata != elf->e_data) {
  92.     _elf_free(elf->e_rawdata);
  93.     }
  94.     if (elf->e_free_syms) {
  95.     _elf_free(elf->e_symtab);
  96.     }
  97.     if (elf->e_free_ehdr) {
  98.     _elf_free(elf->e_ehdr);
  99.     }
  100.     if (elf->e_free_phdr) {
  101.     _elf_free(elf->e_phdr);
  102.     }
  103.     if (elf->e_free_shdr) {
  104.     _elf_free(elf->e_shdr);
  105.     }
  106.     free(elf);
  107.     return 0;
  108. }
  109.